package org.exadel.anibox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.apache.cordova.DroidGap;
import android.os.Bundle;
import android.os.Environment;
import android.view.WindowManager;

public class MainActivity extends DroidGap {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String str = readFileSD();
		if (str.equals("on") == true){
			//add hadware acceleration for app
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
		}
		super.loadUrl("file:///android_asset/www/index.html");
	}

	String readFileSD() {
	    // проверяем доступность SD
	    if (!Environment.getExternalStorageState().equals(
	        Environment.MEDIA_MOUNTED)) {
	      return "";
	    }
	    // получаем путь к SD
	    File sdPath = Environment.getExternalStorageDirectory();
	    // добавляем свой каталог к пути
	    //sdPath = new File(sdPath.getAbsolutePath() + "/" + DIR_SD);
	    sdPath = new File(sdPath.getAbsolutePath()+"/anibox");
	    sdPath.mkdir();
	    // формируем объект File, который содержит путь к файлу
	    File sdFile = new File(sdPath, "anibox.txt");
	    try {
	      // открываем поток для чтения
	      BufferedReader br = new BufferedReader(new FileReader(sdFile));
	      String str = "";
	      // читаем содержимое
	      str = br.readLine();
	      br.close();
	      return str;
	      
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
		return ""; 
	}
	
	//events of buttons
/*	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    //If volumedown key
	    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
	        //this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
	        //load javascript function
	    	this.loadUrl("javascript:unZoom()");
	        return true;
	    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
	        //this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
	    	//load javascript function
	    	this.loadUrl("javascript:Zoom()");
	    	return true;
	    } else {
	        //return super.onKeyDown(keyCode, event); 
	    }
	    //return super.onKeyDown(keyCode, event);

	    return true;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
	    //LOG.d(TAG, "KeyUp has been triggered on the view" + keyCode);
	    
	    // If back key
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        //this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
	    	this.loadUrl("javascript:exitApp();");
	        return true;
	    }
	    // Legacy
	    else if (keyCode == KeyEvent.KEYCODE_MENU) {
	    	//Toast.makeText(getApplicationContext(), "menu", Toast.LENGTH_LONG).show();
	        //this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
	    	this.loadUrl("javascript:Vibrate();");
	    	return true;
	    }
	    // If search key
	    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
	    	//not use
	        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
	        return true;
	    }
	    return false;
	}*/
	
}
